#include "basic.h"
#include <getopt.h>


static const struct option longopts[] =
{
	{ "ui", required_argument, NULL, 'u' },
	{ "output", required_argument, NULL, 'o' },
	{ NULL, 0, NULL, 0 }
};
context_s context;


static void show_context();
//-----------------------------------------------------------------------------------
void get_context( int argc, char *argv[] )
{
	memset( &context, 0, sizeof(context_s) );
	
	int optc;
	while ((optc = getopt_long(argc, argv, "u:o:", longopts, NULL)) != -1){
		switch (optc){
			case 'u':
				context.ui = (char *)malloc(sizeof("ui/") + sizeof(optarg) + sizeof(".so") + 1);
				strcpy(context.ui, "ui/");
				strcat(context.ui, optarg);
				strcat(context.ui, ".so");
				break;
			case 'o':
				context.output = (char *)malloc(sizeof("output/") + sizeof(optarg) + sizeof(".so") + 1);
				strcpy(context.output, "output/");
				strcat(context.output, optarg);
				strcat(context.output, ".so");
				break;
			case '?':
				break;
			default:
				break;
		}
	}
	if( context.ui == NULL || context.output == NULL ){
		printf( "Options --output(-o) and --ui(-u) MUST Be Specified\n" );
		exit(-1);
	}
	
	context.cfg_dir = (char *)malloc( strlen( getenv("HOME") ) + strlen(CFG_DIR) + 1 );
	strcpy( context.cfg_dir, getenv("HOME") );
	strcat( context.cfg_dir, CFG_DIR );
	if( access(context.cfg_dir, F_OK) )
		mkdir(context.cfg_dir, 0x755);
	
	context.lst_dir = (char *)malloc( strlen( context.cfg_dir ) + strlen("playlists/") + 1 );
	strcpy( context.lst_dir, context.cfg_dir );
	strcat( context.lst_dir, "playlists/" );
	if( access(context.lst_dir, F_OK) )
		mkdir(context.lst_dir, 0x755);
	
	context.cfg_file = (char *)malloc( strlen( context.cfg_dir ) + strlen("config.cfg") + 1 );
	strcpy( context.cfg_file, context.cfg_dir );
	strcat( context.cfg_file, "config.cfg" );

	if( ! access("/bin/sandcat", F_OK) )
		context.prefix = "/";
	if( ! access("/usr/bin/sandcat", F_OK) )
		context.prefix = "/usr";
	if( ! access("/usr/local/bin/sandcat", F_OK) )
		context.prefix = "/usr/local";
	if( context.prefix == NULL )
		context.prefix = "/usr/local";
	
	context.lib_dir = (char *)malloc( strlen(context.prefix) + strlen("/lib/sandcat/") + 1 );
	strcpy( context.lib_dir, context.prefix );
	strcat( context.lib_dir, "/lib/sandcat/" );
	
	find_libs( context.lib_dir, "ui", context.libs_ui, sizeof(context.libs_ui) );
	find_libs( context.lib_dir, "output", context.libs_output, sizeof(context.libs_output) );
	find_libs( context.lib_dir, "codec", context.libs_codec, sizeof(context.libs_codec) );
	
	show_context();
}

static void show_context()
{
	printf( "::Show Context::\n" );
	printf( "context.ui : %s\n", context.ui );
	printf( "context.output : %s\n", context.output );
	printf( "context.prefix : %s\n", context.prefix );
	printf( "context.lib_dir : %s\n", context.lib_dir );
	printf( "context.cfg_dir : %s\n", context.cfg_dir );
	printf( "context.cfg_file : %s\n", context.cfg_file );
	printf( "context.lst_dir : %s\n", context.lst_dir );
	int i;
	for( i=0 ; context.libs_ui[i] != 0 ; i++)
		printf( "context.libs_ui[%d] : %s\n", i, context.libs_ui[i] );
	for( i=0 ; context.libs_codec[i] != 0 ; i++)
		printf( "context.libs_codec[%d] : %s\n", i, context.libs_codec[i] );
	for( i=0 ; context.libs_output[i] != 0 ; i++)
		printf( "context.libs_output[%d] : %s\n", i, context.libs_output[i] );
}



